﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Kaidou.Persister;
using Kaidou.Model;
using System.Collections.Generic;
using Kaidou.Util;
using Kaidou.Bll;
using System.Text.RegularExpressions;


public partial class login : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        Session.Timeout = 30;
        if (!IsPostBack)
        {

            if (Request.ServerVariables.Get("HTTPS") == "off")
            {

                Response.Redirect("https://" + Request.ServerVariables.Get("HTTP_HOST") + "/login.aspx", false);

            }

        }


    }

    protected void login_confirm(object sender, EventArgs e)
    {
        string area = Resources.Resource.area;
        string[] areaArray = area.Split(',');

        LoginModel M = new LoginModel();
        M.Password = login_password.Text;
        if (login_userid.Text.Trim().Length > 5)
        {
            M.Unioncode = login_userid.Text.Substring(0, 5);
            string bcode = login_userid.Text.Substring(5, login_userid.Text.Length - 5);
            if (!StringUtil.isHankakuNumber(bcode))
            {
                M.Branchcode = -1;
            }
            else
            {
                M.Branchcode = int.Parse(bcode);
            }
            M.UserID = M.Unioncode + M.Branchcode;
        }
        else
        {
            M.Unioncode = null;
            M.Branchcode = -1;
            M.UserID = M.Unioncode + M.Branchcode;
        }
        LoginManager lm = new LoginManager();
        List<string> errorCodeList = lm.checkAuth(M, Resources.Resource.ResourceManager);
        msg.Text = ErrorCodeUtil.errorCodeListToHtml(errorCodeList);

        if (errorCodeList.Count == 0)
        {
            if (M.Branchcode == 0)
            {
                InputErrorException ie=new InputErrorException(Resources.Resource.ResourceManager);
                MstUnion union=lm.authUnion(M, ie, Resources.Resource.ResourceManager);
                List<string> unionMsg = ie.getErrorMessages();
                msg.Text = ErrorCodeUtil.errorCodeListToHtml(unionMsg);
                if (unionMsg.Count == 0)
                {
                    Session["Login"] = union;
                    Session["Login_Name"] = union.OfficeName + "<br/>本店<br/>";
                    Session["Login_Union_code"] = union.Id.UnionCode;
                    Session["Login_Branch_code"] = union.Id.BranchCode;
                    Session["Login_role"] = union.UserClss;
                    
                    LoginModel loginModel = new LoginModel();
                    loginModel.ParentOfficeName = union.OfficeName;
                    loginModel.OfficeName = "000 本店";
                    loginModel.OfficeTanto = union.TantoName;
                    loginModel.Name = union.OfficeName + "<br/>本店<br/>";
                    loginModel.Unioncode = union.Id.UnionCode; ;
                    loginModel.Branchcode = union.Id.BranchCode;
                    loginModel.Userclss = union.UserClss;
                    loginModel.Parent = union;
                    loginModel.Office_zip_no11 = union.OfficeZipNo.Substring(0, 3);
                    loginModel.Office_zip_no21 = union.OfficeZipNo.Substring(3, 4);
                    loginModel.Office_fax1 = union.OfficeFax;
                    loginModel.Office_city1 = union.OfficeCity;
                    loginModel.Office_pref1 = areaArray[union.OfficePrefCode-1];
                    loginModel.Office_tel1 = union.OfficeTel;
                    loginModel.Office_address1 = union.OfficeCity+ " " + union.OfficeAddress;
                    loginModel.OfficeEmail = union.OfficeEmail;

                    Session["loginModel"] = loginModel;

                    Response.Redirect("menu.aspx");
                }

            }
            else if (M.Branchcode > 0)
            {
                InputErrorException ie = new InputErrorException(Resources.Resource.ResourceManager);
                mst_union_branch branch=lm.authBranch(M, ie, Resources.Resource.ResourceManager);
                List<string> unionMsg = ie.getErrorMessages();
                msg.Text = ErrorCodeUtil.errorCodeListToHtml(unionMsg);
                if (unionMsg.Count == 0)
                {
                    MST_UnionPersister dao = new MST_UnionPersister();
                    MstUnion parent = dao.getDetail(branch.Id.unioncode);
                    Session["Login"] = branch;
                    Session["Login_Name"] = parent.OfficeName + "<br/>" + String.Format("{0:D3} ", branch.Id.branchcode) + " " + branch.officename + "<br/>";
                    Session["Login_Union_code"] = branch.Id.unioncode;
                    Session["Login_Branch_code"] = branch.Id.branchcode;
                    Session["Login_role"] = branch.userclss;

                    LoginModel loginModel = new LoginModel();
                    loginModel.ParentOfficeName = parent.OfficeName;
                    loginModel.OfficeName = StringUtil.getBranchCodeNumberFormat( branch.Id.branchcode) + " " + branch.officename ;
                    loginModel.OfficeTanto = branch.tantoname;
                    loginModel.Name = parent.OfficeName + "<br/>" +StringUtil.getBranchCodeNumberFormat( branch.Id.branchcode) + " " + branch.officename + "<br/>";
                    loginModel.Unioncode = branch.Id.unioncode;
                    loginModel.Branchcode = branch.Id.branchcode;
                    loginModel.Userclss = branch.userclss;
                    loginModel.Parent = parent;
                    loginModel.Office_zip_no11 = branch.officezipno.Substring(0,3);
                    loginModel.Office_zip_no21 = branch.officezipno.Substring(3, 4);
                    loginModel.Office_fax1 = branch.officefax;
                    loginModel.Office_city1 = branch.officecity;
                    loginModel.Office_pref1 = areaArray[branch.officeprefcode-1];
                    loginModel.Office_tel1 = branch.officetel;
                    loginModel.Office_address1 = branch .officecity+ " " + branch.officeaddress;
                    loginModel.OfficeEmail = branch.officeemail;
                    Session["loginModel"] = loginModel;

                    Response.Redirect("menu.aspx");
                }
            }
           
        }


    }
}
